package com.koron.plan.web.repairplan.service.impl;

import com.koron.common.core.util.CodeTools;
import com.koron.plan.web.repairplan.bean.entity.PlanPackageEquipmentBean;
import com.koron.plan.web.repairplan.mapper.PlanPackageEquipmentMapper;
import com.koron.plan.web.repairplan.service.PlanPackageEquipmentService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

/**
 * @author twcao
 * @title: PlanPackageEquipmentServiceImpl
 * @projectName GDH_EAM_V2
 * @description: 检修计划包设备列表service实现
 * @date 2021/9/1314:28
 */
@Service
public class PlanPackageEquipmentServiceImpl implements PlanPackageEquipmentService {
    /**
     * 保存或更新
     *
     * @param factory 会话
     * @param beans   实例
     * @return
     */
    @Override
    public boolean saveOrUpdate(SessionFactory factory, List<PlanPackageEquipmentBean> beans) {
        if(CollectionUtils.isEmpty(beans)) {
            return true;
        }
        PlanPackageEquipmentMapper equipmentMapper = factory.getMapper(PlanPackageEquipmentMapper.class);
        beans.stream().forEach(row -> row.setId(StringUtils.isBlank(row.getId()) ? CodeTools.getCode32() : row.getId()));
        return equipmentMapper.saveOrUpdate(beans);
    }

    /**
     * 根据计划包id删除设备
     * @param factory
     * @param packageId 计划包id
     * @return
     */
    @Override
    public boolean deleteByPackageId(SessionFactory factory, String packageId) {
        Assert.hasText(packageId, "计划包id不能为空");
        PlanPackageEquipmentMapper equipmentMapper = factory.getMapper(PlanPackageEquipmentMapper.class);
        return equipmentMapper.deleteByPackageId(packageId);
    }

    /**
     * 根据计划id删除设备
     * @param factory
     * @param issueId
     * @return
     */
    @Override
    public boolean deleteByIssueId(SessionFactory factory, String issueId) {
        Assert.hasText(issueId, "计划id不能为空");
        PlanPackageEquipmentMapper equipmentMapper = factory.getMapper(PlanPackageEquipmentMapper.class);
        return equipmentMapper.deleteByIssueId(issueId);
    }

    /**
     * 根据计划ids查询设备列表
     * @param factory
     * @param issueId
     * @return
     */
    @Override
    public List<PlanPackageEquipmentBean> queryListByIssueIds(SessionFactory factory, List<String> planIds) {
        if (CollectionUtils.isEmpty(planIds)) {
            return new ArrayList<>();
        }
        PlanPackageEquipmentMapper equipmentMapper = factory.getMapper(PlanPackageEquipmentMapper.class);
        return equipmentMapper.queryListByIssueIds(planIds);
    }
}
